package com.xxq.gulimall.ware.service.impl;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxq.common.utils.PageUtils;
import com.xxq.common.utils.Query;

import com.xxq.gulimall.ware.dao.WareSkuDao;
import com.xxq.gulimall.ware.entity.WareSkuEntity;
import com.xxq.gulimall.ware.service.WareSkuService;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        Long wareId = (Long) params.get("wareId");
        Long skuId = (Long) params.get("skuId");
        if (wareId != null && wareId == 0 && skuId != null && skuId != 0) {
            HashMap<String, Long> id = new HashMap<>();
            id.put("ware_id", wareId);
            id.put("sku_id", skuId);
            queryWrapper.allEq(id);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Map<Long, Boolean> getStocks(List<Long> skuIds) {
        List<WareSkuEntity> stock = baseMapper.getStock(skuIds);
        Map<Long, Boolean> map = new HashMap<>();
        stock.forEach(entity -> {
            if (entity.getStock() > 0)
                map.put(entity.getSkuId(), Boolean.TRUE);
            else
                map.put(entity.getSkuId(), Boolean.FALSE);
        });

        return map;
    }

}